package com.ziki.bswicket.template.form.page;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.CompoundPropertyModel;

import com.ziki.bswicket.action.SubmitAction;
import com.ziki.bswicket.component.BsDropDown;
import com.ziki.bswicket.component.BsForm;
import com.ziki.bswicket.component.BsSubmitLink;
import com.ziki.bswicket.component.BsTextField;
import com.ziki.bswicket.template.base.BaseTemplatePage;
import com.ziki.bswicket.template.form.model.FormModel;
import com.ziki.framework.wicket.markup.html.form.FwChoiceBean;
import com.ziki.framework.wicket.markup.html.form.FwChoiceData;

public class DropDownPage extends BaseTemplatePage {
    private static final long serialVersionUID = 1L;

    public DropDownPage() {
        FormModel formModel = new FormModel();
        // 设置默认值
        FwChoiceBean selectA = new FwChoiceBean();
        selectA.setId("id3");
        selectA.setValue("value3");
        formModel.setSelectA(selectA);

        CompoundPropertyModel<FormModel> model = new CompoundPropertyModel<>(formModel);
        BsForm<FormModel> form = new BsForm<>("form", model);
        add(form);
        form.add(new BsTextField<String>("title"));

        // 下拉List
        List<FwChoiceBean> lstChoice = new ArrayList<>();
        FwChoiceBean choice;
        for (int i = 0; i < 4; i++) {
            choice = new FwChoiceBean();
            choice.setId("id" + i);
            choice.setValue("value" + i);
            lstChoice.add(choice);
        }

        FwChoiceData<FwChoiceBean> choices = new FwChoiceData<FwChoiceBean>(lstChoice);
        form.add(new BsDropDown<FwChoiceBean>("selectA", choices));

        BsSubmitLink submit = new BsSubmitLink("submit");
        submit.add(submitAction(form.getModelObject()));
        form.add(submit);
    }

    private SubmitAction submitAction(final FormModel model) {
        return new SubmitAction() {
            public void performSubmit() {
                System.out.println(model.getTitle());
                System.out.println(model.getSelectA().getId());
                System.out.println(model.getSelectA().getValue());
            }
        };
    }
}
